package com.ccg.plat.presentation.ui.feature3.viewmodel

import android.app.Activity
import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.ccg.plat.data.local.cache.UserInfoCache
import com.ccg.plat.presentation.common.base.viewmodel.BaseViewModelC
import com.ccg.plat.presentation.common.util.NavigationUtils
import com.ccg.plat.presentation.ui.feature3.model.RoomZipBean
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow

/**
 * @author : C4_雍和
 * 描述 :
 * 主要功能 :
 * 维护人员 : C4_雍和
 * date : 2025/8/9 8:45
 */
class RoomListViewModel(application: Application) : BaseViewModelC(application) {
    /**
     * 定义意图
     */
    sealed class ContentIntent {
        /**
         * 跳转到房间列表页面
         * @property activity Activity
         * @property position Int
         * @constructor
         */
        class GoVideoTagActivity(val activity: Activity, val position: Int) : ContentIntent()
    }

    data class NaviViewState(var uiListData: List<RoomZipBean> = emptyList())

    val newsChannel = Channel<ContentIntent>(Channel.UNLIMITED)
    var uiState by mutableStateOf(NaviViewState())

    init {
        handleIntent()
    }

    private fun handleIntent() {
        launch(Dispatchers.IO) {
            newsChannel.consumeAsFlow().collect { data ->
                when (data) {
                    is ContentIntent.GoVideoTagActivity -> {
                        NavigationUtils.goVideoTagActivity(context = data.activity, json = GsonBuilder().create().toJson(uiState.uiListData[data.position]))
                    }
                }
            }
        }
    }

    suspend fun initData() {
        var netJson = ""
        val saveJson = MMKV.defaultMMKV().decodeString("roomlist", "")
        if (saveJson.isNullOrEmpty()) {
            netJson = retrofit.getNetData("${UserInfoCache.getBaseUrl()}/roomlist.json").string()
            MMKV.defaultMMKV().encode("roomlist", netJson)
            val timeStamp1 = System.currentTimeMillis()
            MMKV.defaultMMKV().encode("roomTime", timeStamp1)
        } else {
            val mTime = MMKV.defaultMMKV().decodeLong("roomTime", 0)
            val currentTime = System.currentTimeMillis()
            if (currentTime > (mTime + 86400000)) {
                //如果当前时间戳>保存的时间戳+24小时
                //http://117.72.114.24:8083/xiaohuanger/roomlist.json
                netJson = retrofit.getNetData("${UserInfoCache.getBaseUrl()}/roomlist.json").string()
                if (saveJson != netJson) {
                    //本地存储的数据做对比网络请求的数据两条数据不一样,说明视频更新了
                    MMKV.defaultMMKV().encode("roomlist", netJson)
                    val timeStamp1 = System.currentTimeMillis()
                    MMKV.defaultMMKV().encode("roomTime", timeStamp1)
                }
            }
        }
        val str = netJson.ifEmpty {
            saveJson
        }
        val mList = GsonBuilder().create().fromJson<List<RoomZipBean>>(str, object : TypeToken<List<RoomZipBean>>() {}.type)
        uiState = uiState.copy(uiListData = mList)
    }
}